<?php

namespace App\Listeners;

use App\Enum\LogsEnum;
use App\Enum\OrderEnum;
use App\Events\DriverStart;
use App\Models\LogsModel;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;

class DriverStartNotification
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     *
     * @param  DriverStart  $event
     * @return void
     */
    public function handle(DriverStart $event)
    {
        $fromBackend = false;$adminId = 0;$adminName='';

        //如果是后台请求取消订单
        if( isset($event->adminInfo['admin_id']) && !empty($event->adminInfo['admin_id'])){
            $adminId    = $event->adminInfo['admin_id'];
            $adminName  = $event->adminInfo['admin_name'];
            $fromBackend = true;
        }

        // 添加订单日志 20230901 by LC
        LogsModel::query()->create([
            'type'      => LogsEnum::LOGS_TYPE_ORDER,
            'ref_id'    => $event->orderInfo->id,
            'ref_no'    => $event->orderInfo->order_no,
            'user_id'   => $fromBackend?$adminId:$event->orderInfo->driver_id,
            'user_type' => $fromBackend?LogsEnum::LOGS_USER_TYPE_BACKEND:LogsEnum::LOGS_USER_TYPE_DRIVER,
            'username'  => $fromBackend?$adminName:LogsEnum::LOGS_USER_NAME,
            'content'   => "司机服务。状态：".OrderEnum::STATUS_NAME[$event->orderInfo->old_status].'->'.OrderEnum::STATUS_NAME[$event->orderInfo->status],
        ]);
    }
}
